<template>
  <div class="login_container">
    <div class="form_box">
      <div class="logon_title">系统登录页面</div>
      <el-form :model="formData" :rules="formRules" :size="formSize" ref="ruleFormRef">
        <el-form-item prop="userName">
          <el-input v-model="formData.userName" placeholder="账号" clearable prefix-icon="UserFilled" :style="{ width: '100%', height: '38px' }"></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input
            v-model="formData.passWord"
            placeholder="密码"
            :maxlength="6"
            clearable
            prefix-icon="Cellphone"
            suffix-icon="el-icon-view"
            show-password
            :style="{ width: '100%', height: '38px' }"
          >
          </el-input>
        </el-form-item>
        <div class="codeBox">
          <el-form-item prop="code" style="margin-bottom: 0px">
            <el-input v-model="formData.code" placeholder="验证码" clearable prefix-icon="Notification" :style="{ width: '100%', height: '38px' }"></el-input>
          </el-form-item>
          <el-image
            src=""
            class="codeBox_image"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><ChatSquare /></el-icon>
              </div>
            </template>
          </el-image>
        </div>
      </el-form>

      <div class="btn_box">
        <el-button type="primary" style="width: 100%; height: 36px" :loading="isLoading" @click="goLogin(ruleFormRef)">Login</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
//导入状态管理
import { useStore } from '@/store/user.js';
import { useRouter } from 'vue-router';

const useStoreRR = useStore();
//表单尺寸
const formSize = ref('default');
//路由
const router = useRouter();
//表单数据
var formData = reactive({
  passWord: '',
  userName: '',
  code: ''
});

//表单验证 规则
const ruleFormRef = ref();
var formRules = reactive({
  userName: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  passWord: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    },
    { min: 6, max: 6, message: 'Length should be 6', trigger: 'blur' }
  ],
  code: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'blur'
    }
  ]
});

//登录按钮loading
var isLoading = ref(false);
//登录方法
const goLogin = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log('submit!');
      isLoading.value = true;
      //登录成功设置token
      await useStoreRR.logonSet();
      console.log('useStoreRRtk', useStoreRR.tk);
      //查询用户信息设置用户信息
      await useStoreRR.getUserInfo(useStoreRR.tk);
      console.log('----------111');
      router.replace({
        path: '/'
      });

      //  useStoreRR.getUserInfo()
      // isLoading.value = false
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-image: url('https://img.zcool.cn/community/018bea61cf1d8211013f01cd4f900f.jpg?x-oss-process=image/format,webp/quality,q_100');
  background-size: cover;
  background-color: #000;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .form_box {
    width: 330px;
    padding: 16px;
    min-height: 320px;
    background: #fff;
    border-radius: 4px;
    .logon_title {
      font-size: 24px;
      font-weight: bold;
      text-align: center;
      margin-bottom: 16px;
    }
    .codeBox {
      display: flex;
      align-items: center;
      margin-bottom: 18px;
      .codeBox_image {
        background: #ccc;
        width: 33%;
        height: 38px;
        margin-left: 4px;
      }
    }
    .btn_box {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
